package com.apache.member.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.cache.util.Validator;
import com.apache.member.common.ConstantData;
import com.apache.member.entity.MemberType;
import com.apache.member.entity.MemberTypeVo;
import com.apache.member.manager.MemberTypeManager;

/**
 * 数据字典控制器
 * description:  
 * @author lxsong 创建时间：2015-6-16  
 * @Copyright(c)2014:北京软件技术有限公司
 */
@Controller
@RequestMapping("/dict")
public class MemberTypeController {

	@Autowired
	MemberTypeManager memberTypeManager;

	/**
	 * 会员类型删除
	 * description:  
	 * @param typeNo
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/delete.action")
	@ResponseBody
	public ResultMsg delete(String typeNo) {

		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setInfoId(typeNo);
		boolean deleteInfo = memberTypeManager.deleteInfo(vo);
		if (deleteInfo) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}

		return resultMsg;
	}

	/**
	 * 跳转到会员类型列表界面
	 * description:  
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/toList")
	public ModelAndView toList() {
		//TODO 待删除
		ModelAndView model = new ModelAndView("/dict/memberType/list");
		return model;
	}

	/**
	 * 展示会员类型列表
	 * description:  
	 * @param memberType
	 * @param request
	 * @param page
	 * @param rows
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/list.action")
	@ResponseBody
	public Map<String, Object> list(String id) {
		String parentId = Validator.getDefaultStr(id, "0");
		MemberType memberType = new MemberType();
		memberType.setParentId(parentId);
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setObj(memberType);
		List<MemberType> list = memberTypeManager.getList(vo);
		//List<MemberTypeVo> memberTypeList = this.getSubMemberTypeList(list);
		//List<MemberTypeVo> li = new ArrayList<MemberTypeVo>();
		List<MemberTypeVo> subMemberTypeList = this.getSubMemberTypeList(list);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("rows", subMemberTypeList);
		return mapData;
	}

	public List<MemberTypeVo> getSubMemberTypeList(List<MemberType> list) {
		List<MemberTypeVo> reVo = new ArrayList<MemberTypeVo>();
		for (MemberType mem : list) {
			MemberType memberType1 = new MemberType();
			memberType1.setParentId(mem.getTypeNo());
			ParamsVo<MemberType> vo1 = new ParamsVo<MemberType>();
			vo1.setObj(memberType1);
			long countInfo = memberTypeManager.countInfo(vo1);
			if (countInfo > 0) {
				List<MemberType> list2 = memberTypeManager.getList(vo1);
				MemberTypeVo v = new MemberTypeVo();
				v.setTypeNo(mem.getTypeNo());
				v.setTypeName(mem.getTypeName());
				v.setParentId(mem.getParentId());
				List<MemberTypeVo> list3 = this.getSubMemberTypeList(list2);
				v.setChildren(list3);
				reVo.add(v);
			} else {
				MemberTypeVo vo = new MemberTypeVo();
				vo.setTypeNo(mem.getTypeNo());
				vo.setTypeName(mem.getTypeName());
				vo.setParentId(mem.getParentId());
				reVo.add(vo);
			}
		}

		return reVo;
	}

	/**
	 * 
	 * 会员注册页面，拿到会员类型 
	 * @param request
	 * @param response
	 * @param memberType
	 * @return  
	 * @author yaojs 
	 * @update 2015-6-18
	 */
	@RequestMapping("/memberType/toAddMember.action")
	@ResponseBody
	public List<String> getMemberTypeList(MemberType memberType, HttpServletRequest request,
			HttpServletResponse response) {
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		//查询所有的一级节点
		memberType.setParentId("0");
		vo.setObj(memberType);
		List<MemberType> memberTypeList = memberTypeManager.getList(vo);
		List<MemberType> memberTypeList1 = new ArrayList<MemberType>();
		if (!Validator.isEmpty(memberTypeList)) {
			for (int i = 0; i < memberTypeList.size(); i++) {
				// 拿到所有的父节点为0的节点
				if (null != memberTypeList.get(i).getParentId() && "0".equals(memberTypeList.get(i).getParentId())) {
					MemberType memberType1 = new MemberType();
					memberType1.setParentId(memberTypeList.get(i).getTypeNo());
					ParamsVo<MemberType> vo1 = new ParamsVo<MemberType>();
					vo1.setObj(memberType1);
					// 查询该父节点的子节点
					List<MemberType> list1 = memberTypeManager.getList(vo1);
					// 如果父节点没有子节点，则添加该父节点，否则遍历添加子节点
					if (list1.size() == 0) {
						memberTypeList1.add(memberTypeList.get(i));
					} else {
						for (MemberType mt : list1) {
							memberTypeList1.add(mt);
						}
					}
				}
			}
		} else {
			return null;
		}
		List<String> list = new ArrayList<String>();
		for (MemberType mt : memberTypeList1) {
			String str = mt.getTypeNo() + "|" + mt.getTypeName();
			list.add(str);
		}
		return list;
	}

	/**
	 * 跳转到会员保存界面
	 * description:  
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/toSave")
	public ModelAndView toSave(String parentId) {

		ModelAndView model = new ModelAndView("dict/memberType/memberType-save", "parentId", parentId);

		return model;
	}

	/**
	 * 会员类型保存
	 * description:  
	 * @param memberType
	 * @param request
	 * @param response
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	//@Token(remove = true)
	//防表单重复提交注解
	@RequestMapping("/memberType/save.action")
	@ResponseBody
	public ResultMsg save(MemberType memberType, HttpServletRequest request, HttpServletResponse response) {
		/*LoginUser loginUser = PBOSSOTools.getLoginUser(request);*/
		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		String typeNo = memberType.getTypeNo();
		ParamsVo<MemberType> v = new ParamsVo<MemberType>();
		v.setInfoId(typeNo);
		MemberType m = (MemberType) memberTypeManager.getInfoById(v);
		if (m != null) {
			resultMsg.setFlag(ConstantData.FLAG_FALSE);
			resultMsg.setMsg("该类型编号已存在");
			return resultMsg;
		}
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setObj(memberType);
		String saveInfo = memberTypeManager.saveInfo(vo);
		if (Validator.isNotNull(saveInfo)) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功！");
		}
		return resultMsg;
	}

	/**
	 * 跳转到会员类型编辑页面
	 * description:  
	 * @param typeNo
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/toEdit.action")
	@ResponseBody
	public Map<String, Object> toEdit(String typeNo) {

		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setInfoId(typeNo);

		MemberType memberType = (MemberType) memberTypeManager.getInfoById(vo);
		Map<String, Object> mapData = new HashMap<String, Object>();
		mapData.put("memberType", memberType);
		return mapData;
	}

	/**
	 * 会员类型修改
	 * description:  
	 * @param request
	 * @param response
	 * @param memberType
	 * @return  
	 * @author lxsong
	 * @update 2015-6-16
	 */
	@RequestMapping("/memberType/edit.action")
	@ResponseBody
	public ResultMsg edit(HttpServletRequest request, HttpServletResponse response, MemberType memberType) {

		ResultMsg resultMsg = new ResultMsg();
		resultMsg.setFlag(ConstantData.FLAG_FALSE);
		resultMsg.setMsg("操作失败");
		ParamsVo<MemberType> vo = new ParamsVo<MemberType>();
		vo.setObj(memberType);
		boolean editInfo = memberTypeManager.editInfo(vo);
		if (editInfo) {
			resultMsg.setFlag(ConstantData.FLAG_OK);
			resultMsg.setMsg("操作成功");
		}

		return resultMsg;
	}

}
